let baseUrlDev = "http://zyr.jiuhuakang.com";
let baseUrlDev1 = "https://testapi.ygcha.com.cn";
let lmUrl = 'http://192.168.5.37:8772'
let baseUrl1 = "https://aq.ygcha.com.cn";
let baseUrl2 = "https://qq.tlcha.com.cn";
let baseUrl3 = "https://aq.ruinuomei.com";
let baseUrl4 = "https://aq.jiuhuakang.com";

var httpurl = baseUrl4;
// var visitimgurl = "http://small.tlcha.com.cn";
// let visitimgurl = "http://small.muruji.com";
// let visitimgurl = "http://small.ygcha.com.cn";
let visitimgurl = "http://small.jiuhangyunxuan.cn";

let getImgpageNo = 1,
    getImgpageSize = 24,
    totalRecords = 0,
    activityId = 0;
let activityTime = "",
    activityName = "",
    note = "",
    enable = 0;
let contents = '<div class="form-group-col-2">\n' +
    '                    <div class="form-label">启用时间：</div>\n' +
    '                    <div class="form-cont">\n' +
    '                        <input type="text" id="activityTime" onfocus="updateActivityTime();" class="form-control width100 laydate-icon" placeholder=" 时间起始-时间结束 ">\n' +
    '                    </div>\n' +
    '                </div>\n' +
    '                <div class="form-group-col-2">\n' +
    '                    <div class="form-label">模块名称：</div>\n' +
    '                    <div class="form-cont">\n' +
    '                        <input id="activityName"  class="form-control laydate-icon width100" type="text" value="" placeholder="模块名称" />\n' +
    '                    </div>\n' +
    '                </div>\n' +
    '                <div class="form-group-col-2">\n' +
    '                    <div class="form-label">备注：</div>\n' +
    '                    <div class="form-cont">\n' +
    '                        <input id="note"  class="form-control laydate-icon width100" type="text" value="" placeholder="备注" />\n' +
    '                    </div>\n' +
    '                </div>\n' +
    '                <div class="form-group-col-2">\n' +
    '                    <div class="form-label">优先级：</div>\n' +
    '                    <div class="form-cont">\n' +
    '                        <input id="sort"  class="form-control laydate-icon width100" type="text" value="" placeholder="数值越大,优先级越高" />\n' +
    '                    </div>\n' +
    '                </div>\n' +
    '                <div class="form-group-col-2">\n' +
    '                    <div class="form-label">是否启用：</div>' +
    '                    <div class="form-cont" class="enable">\n' +
    '                                            <label class="radio">\n' +
    '                                                <input type="radio" value="1" name="enable"/><span>启用</span>\n' +
    '                                            </label>\n' +
    '                                            <label class="radio">\n' +
    '                                                <input type="radio" value="0" checked="checked" name="enable"/><span>不启用</span>\n' +
    '                                            </label>\n' +
    '                                        </div>' +
    '                </div>\n' +
    '                <div class="form-group-col-2">\n' +
    '                    <div class="form-label">是否为默认模板：</div>' +
    '                    <div class="form-cont" class="defaulti">\n' +
    '                                            <label class="radio">\n' +
    '                                                <input type="radio" value="1" name="defaulti"/><span>默认模板</span>\n' +
    '                                            </label>\n' +
    '                                            <label class="radio">\n' +
    '                                                <input type="radio" value="0" checked="checked" name="defaulti"/><span>普通模板</span>\n' +
    '                                            </label>\n' +
    '                                        </div>' +
    '                </div>\n' +
    '                <div class="form-group-col-2">\n' +
    '                    <div class="form-label"></div>\n' +
    '                    <div class="form-cont">\n' +
    '                        <input type="button" value="确认" class="btn btn-info" onclick="sureSetting()"/>\n' +
    '                        <input type="button" value="返回" class="btn btn-danger" onclick="return layer.close(index);" />\n' +
    '                    </div>\n' +
    '                </div>';

function closeAllLayer() {
    top.layer.closeAll();
    layer.closeAll();
}
let chooseTypeList = [];

function chooseThis(obj) {
    let tindex = $(obj).parents("tr").data("index");
    let objres = resList[tindex];
    let flag = false;
    let dataId = objres.id;
    let ob = { "id": objres.id, "img": objres.img, "name": objres.name };
    console.log(chooseTypeList, "===cho");
    for (let a = 0; a < chooseTypeList.length; a++) {
        if (chooseTypeList[a].id == dataId) {
            chooseTypeList.splice(a, 1);
            flag = true;
            break;
        }
    }
    if (flag) {
        $(obj).addClass("btn-info");
        $(obj).removeClass("btn-danger");
        $(obj).html("选取");
    } else {
        console.log(ob, "====objres");
        chooseTypeList.push(ob);
        $(obj).addClass("btn-danger");
        $(obj).removeClass("btn-info");
        $(obj).html("已选取");
    }
}
//分页
function page() {
    laypage.render({
        elem: 'page' //注意，这里的 test1 是 ID，不用加 # 号
            ,
        count: totalRecords //数据总数，从服务端得到
            ,
        layout: ['count', 'prev', 'page', 'next', 'refresh', 'skip'],
        curr: pageNo //获取hash值为fenye的当前页
            ,
        hash: 'fenye' //自定义hash值
            ,
        limit: pageSize,
        jump: function(obj, first) {
            //首次不执行
            if (!first) {
                pageNo = obj.curr; // 当前页数;
                pageSize = obj.limit; // 当前页面记录数
                $(".jGetgood").click();
            }
        }
    });
}
let listId = [];
let resList = [];

function selectChooseCouponList() {
    let name = $("#title").val();
    $.ajax({
        url: httpurl + HiShop.Config.AjaxUrl.CouponListByBid,
        dataType: "json",
        data: {
            pageNo: pageNo,
            pageSize: pageSize,
            bid: bid,
        },
        headers: {
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Methods": "*",
            "Access-Control-Allow-Headers": "Authorization",
            "Authorization": GetTokenInfo()
        },
        contentType: 'application/json',
        success: function(a) {
            let dataInfo = [];
            if (200 == a.rtnCode) {
                totalRecords = a.data.total;
                dataInfo = a.data.list;
            } else {
                HiShop.hint("danger", "对不起，未找到任何数据!")
            }
            resList = [];
            table.render({
                elem: '#tableInfo',
                data: dataInfo,
                id: "tableInfoId",
                cols: [
                    [
                        { type: 'checkbox' }, { field: 'id', align: "center", title: 'ID', sort: true }, {
                            field: 'img',
                            align: "center",
                            title: 'ID',
                            sort: true,
                            templet: function(res) {
                                return '<img src="' + visitimgurl + "/" + res.img + '"/>';
                            }
                        }, { field: 'name', align: "center", title: '名称', sort: true }, { field: 'newprice', align: "center", title: '普通价格', sort: true }, { field: 'cpikePrice', align: "center", title: '秒杀价', sort: true }, {
                            field: 'del',
                            align: "center",
                            width: 120,
                            title: '操作',
                            unresize: true,
                            templet: function(res) {
                                let info = "";
                                let flag = false;
                                for (let a = 0; a < chooseTypeList.length; a++) {
                                    if (chooseTypeList[a].id == res.id) {
                                        // chooseTypeList.splice(a,1);
                                        flag = true;
                                        break;
                                    }
                                }
                                let binfo = "";
                                let btext = "";
                                if (flag) {
                                    binfo = "btn-danger";
                                    btext = "已选取";
                                } else {
                                    binfo = "btn-info";
                                    btext = "选取";
                                }
                                resList.push(res);
                                info = '<span class="btn ' + binfo + '" onclick="chooseThis(this)">' + btext + '</span>';
                                return info;
                            }
                        }
                    ]
                ]
            });
            page("selectChooseTypeList");
        }
    })
}

function selectChooseTypeList() {
    let name = $("#title").val();
    $.ajax({
        url: httpurl + HiShop.Config.AjaxUrl.GoodsList,
        dataType: "json",
        headers: {
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Methods": "*",
            "Access-Control-Allow-Headers": "Authorization",
            "Authorization": GetTokenInfo()
        },
        contentType: 'application/json',
        data: {
            pageNo: pageNo,
            pageSize: pageSize,
            name: name,
            type: 1, //秒杀商品
        },
        success: function(a) {
            let dataInfo = [];
            if (200 == a.rtnCode) {
                totalRecords = a.data.total;
                dataInfo = a.data.list;
            } else {
                HiShop.hint("danger", "对不起，未找到任何数据!")
            }
            resList = [];
            table.render({
                elem: '#tableInfo',
                data: dataInfo,
                id: "tableInfoId",
                cols: [
                    [
                        // {type: 'checkbox'}
                        { field: 'id', align: "center", title: 'ID', sort: true }, { field: 'name', align: "center", title: '名称', sort: true }, {
                            field: 'img',
                            align: "center",
                            title: '图片',
                            sort: true,
                            templet: function(res) {
                                return '<img src="' + visitimgurl + "/" + res.img + '"/>';
                            }
                        }, { field: 'newprice', align: "center", title: '原价', sort: true }, { field: 'cpikePrice', align: "center", title: '秒杀价', sort: true }, { field: 'del', align: "center", width: 200, title: '操作', templet: '#barDemo', unresize: true }
                    ]
                ]
            });
            page("selectChooseTypeList");
        }
    })
}

function uploadImg(obj, tempName) {
    let files = $("#file").get(0).files;
    let data = new FormData();
    data.append('file', files[0]);
    let img = "";
    $.ajax({
        type: "POST",
        url: httpurl + '/uploadImgQiniu?pathName=' + tempName + "&divisor=0&dividend=1",
        data: data,
        cache: false,
        async: false,
        processData: false,
        contentType: false,
        success: function(data) {
            if (data.rtnCode == 200) {
                img = visitimgurl + "/" + data.msg;
            } else {
                // top.layer.alert(data.msg, function (index) {
                //     layer.close(index);
                // });
            }
        },
        error: function() {
            //console.log("==error==");
        }
    });
    return img;

}

function selectChooseHeadlinesList() {
    let name = $("#title").val();
    $.ajax({
        url: httpurl + HiShop.Config.AjaxUrl.ActicleList,
        dataType: "json",
        data: {
            "pageNo": parseInt(pageNo),
            "pageSize": pageSize,
            "name": name
        },
        headers: {
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Methods": "*",
            "Access-Control-Allow-Headers": "Authorization",
            "Authorization": GetTokenInfo()
        },
        contentType: 'application/json',
        success: function(a) {
            let dataInfo = [];
            if (200 == a.rtnCode) {
                totalRecords = a.data.total;
                dataInfo = a.data.list;
            } else {
                HiShop.hint("danger", "对不起，未找到任何数据!")
            }
            resList = [];
            table.render({
                elem: '#tableInfo',
                data: dataInfo,
                id: "tableInfoId",
                cols: [
                    [
                        { type: 'checkbox' }, { field: 'name', align: "center", title: '头条文章', sort: true }, {
                            field: 'pic',
                            align: "center",
                            title: '主图',
                            sort: true,
                            templet: function(res) {
                                return '<img src="' + visitimgurl + "/" + res.pic + '"/>';
                            }
                        }, {
                            field: 'del',
                            align: "center",
                            width: 120,
                            title: '操作',
                            unresize: true,
                            templet: function(res) {
                                let info = "";
                                let flag = false;
                                for (let a = 0; a < chooseTypeList.length; a++) {
                                    if (chooseTypeList[a].id == res.id) {
                                        // chooseTypeList.splice(a,1);
                                        flag = true;
                                        break;
                                    }
                                }
                                let binfo = "";
                                let btext = "";
                                if (flag) {
                                    binfo = "btn-danger";
                                    btext = "已选取";
                                } else {
                                    binfo = "btn-info";
                                    btext = "选取";
                                }
                                resList.push(res);
                                info = '<span class="btn ' + binfo + '" onclick="chooseThis(this)">' + btext + '</span>';
                                return info;
                            }
                        }
                    ]
                ]
            });
            page('selectChooseHeadlinesList');
        }
    })
}

function page(tempName) {
    laypage.render({
        elem: 'page' //注意，这里的 test1 是 ID，不用加 # 号
            ,
        count: totalRecords //数据总数，从服务端得到
            ,
        layout: ['count', 'prev', 'page', 'next', 'refresh', 'skip'],
        curr: pageNo //获取hash值为fenye的当前页
            ,
        hash: 'fenye' //自定义hash值
            ,
        limit: pageSize,
        jump: function(obj, first) {
            //首次不执行
            if (!first) {
                pageNo = obj.curr; // 当前页数;
                pageSize = obj.limit; // 当前页面记录数
                if (tempName == 'selectChooseHeadlinesList') {
                    selectChooseHeadlinesList();
                } else if (tempName == 'selectChooseTypeList') {
                    selectChooseTypeList();
                }
                // selectTypeList();
            }
        }
    });
}

function pageAlert() {
    laypage.render({
        elem: 'pageAlert' //注意，这里的 test1 是 ID，不用加 # 号
            ,
        count: totalRecords //数据总数，从服务端得到
            ,
        layout: ['count', 'prev', 'page', 'next', 'refresh', 'skip'],
        curr: pageNo //获取hash值为fenye的当前页
            ,
        hash: 'fenye' //自定义hash值
            ,
        limit: pageSize,
        jump: function(obj, first) {
            //首次不执行
            if (!first) {
                pageNo = obj.curr; // 当前页数;
                pageSize = obj.limit; // 当前页面记录数
                $(".jGetgood").click();
            }
        }
    });
}

function GetImgListPage() {
    laypage.render({
        elem: 'page' //注意，这里的 test1 是 ID，不用加 # 号
            ,
        count: getImgtotalRecords //数据总数，从服务端得到
            ,
        layout: ['count', 'prev', 'page', 'next', 'refresh', 'skip'],
        curr: getImgpageNo //获取hash值为fenye的当前页
            ,
        hash: 'fenye' //自定义hash值
            ,
        limit: getImgpageSize,
        jump: function(obj, first) {
            //首次不执行
            if (!first) {
                getImgpageNo = obj.curr; // 当前页数;
                getImgpageSize = obj.limit; // 当前页面记录数
                GetImgList($("#albums"), Cache.folderID);
            }
        }
    });
}
$(function() {
    document.onmousedown = function mdClick(event) {
        var e = event || window.event || arguments.callee.caller.arguments[0];
        if (e.button == 2 || e.button == 3) {
            mAlert();
        }
    };
    document.oncontextmenu = new Function("return false;");
    document.onkeydown = document.onkeyup = document.onkeypress = function(event) {
        var e = event || window.event || arguments.callee.caller.arguments[0];

        if (e && e.keyCode == 123) {
            mAlert();
            e.returnValue = false;
            return (false);
        }
    };
})

function isNotNull(str) {
    if (str != null && str != undefined && str != '') return true;
    return false;
}

function GetQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}

//获取父页面的url参数的值
function GetParentQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.parent.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}

// 键盘输入限制-正整数
function onkeyNumPositiveInteger(e){
    console.log(e);
    if(e.value.length==1){e.value=e.value.replace(/[^1-9]/g,'')}else{e.value=e.value.replace(/\D/g,'')}
}

// 键盘输入限制-正数
